package com.hctforgreen.greenservice.ui.adapter;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.hctforgreen.greenservice.FeedbackHistoryActivity;
import com.hctforgreen.greenservice.R;
import com.hctforgreen.greenservice.SubmitFeedBackActivityV3;
import com.hctforgreen.greenservice.db.DBFeedbackInfoManager;
import com.hctforgreen.greenservice.model.BaseEntity;
import com.hctforgreen.greenservice.model.FeedBackListEntity.FeedBackEntity;
import com.hctforgreen.greenservice.ui.handler.FeedBackListHandler;
import com.hctforgreen.greenservice.ui.page.AbstractDataLoaderHandler;
import com.hctforgreen.greenservice.ui.page.AbstractPageableAdapter;

public class FeedBackHistoryListAdapter extends
		AbstractPageableAdapter<BaseEntity> implements OnItemClickListener,
		OnItemLongClickListener {

	private Activity mActivity;
	private View mConvertView;
	private ListView mListView;
	private FeedBackListHandler mHandler;

	public FeedBackHistoryListAdapter(ListView listView, Activity activity,
			View convertView, int loadingViewResourceId,
			int reloadViewResourceId,
			AbstractDataLoaderHandler<BaseEntity> handler) {
		super(listView, activity, loadingViewResourceId, reloadViewResourceId,
				handler);
		mListView = listView;
		mActivity = activity;
		mConvertView = convertView;

		mHandler = (FeedBackListHandler) handler;
		mListView.setAdapter(this);
		mListView.setOnScrollListener(this);
		mListView.setOnItemClickListener(this);
		mListView.setOnItemLongClickListener(this);
		mListView.setDivider(mActivity.getResources().getDrawable(
				R.drawable.ic_driver));
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewCache viewCache;
		final FeedBackEntity entity = (FeedBackEntity) getItem(position);
		if (convertView == null) {
			convertView = View.inflate(mActivity,
					R.layout.item_feed_back_history_lst, null);
			viewCache = new ViewCache(convertView);
			convertView.setTag(viewCache);
		} else {
			viewCache = (ViewCache) convertView.getTag();
		}

		viewCache.getSeriesNameTv().setText(entity.seriesName);
		viewCache.getSubmitDateTv().setText(entity.submitDate);
		viewCache.getContentTv().setText(
				mActivity.getString(R.string.feedback_adapter_content_tag)
						+ entity.content);
		viewCache.getTitleTv().setText(
				mActivity.getString(R.string.feedback_adapter_theme_tag)
						+ entity.title);
		viewCache.baseView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				final FeedBackEntity entity = (FeedBackEntity) getItem(position);
				Intent intent = new Intent(mActivity,
						SubmitFeedBackActivityV3.class);
				intent.putExtra("feedbackEntity", entity);
				// mActivity.setResult(Activity.RESULT_OK, intent);
				mActivity.startActivityForResult(intent,
						FeedbackHistoryActivity.REQUEST_CODE);

			}
		});

		viewCache.baseView
				.setOnLongClickListener(new View.OnLongClickListener() {

					@Override
					public boolean onLongClick(View v) {

						AlertDialog.Builder builder = new Builder(mActivity);
						builder.setMessage(
								mActivity
										.getString(R.string.feedback_delete_message))
								.setTitle(
										mActivity
												.getString(R.string.dialog_default_title_hint))
								.setNegativeButton(
										mActivity
												.getString(R.string.dialog_cancel_hint),
										null)
								.setPositiveButton(
										mActivity
												.getString(R.string.dialog_confirm_hint),
										new DialogInterface.OnClickListener() {

											@Override
											public void onClick(
													DialogInterface dialog,
													int which) {
												DBFeedbackInfoManager dbFeedBackController = new DBFeedbackInfoManager(
														mActivity);
												dbFeedBackController
														.delete((FeedBackEntity) getItem(position));
												mHandler.refersh();
											}
										}).create().show();

						return false;

					}
				});
		return convertView;
	}

	class ViewCache {
		private View baseView;
		private TextView submitDate;
		private TextView title;
		private TextView content;
		private TextView seriesName;

		public ViewCache(View view) {
			baseView = view;
		}

		public TextView getSubmitDateTv() {
			if (submitDate == null) {
				submitDate = (TextView) baseView
						.findViewById(R.id.tv_submit_date);
			}
			return submitDate;
		}

		public TextView getTitleTv() {
			if (title == null) {
				title = (TextView) baseView.findViewById(R.id.tv_title);
			}
			return title;
		}

		public TextView getContentTv() {
			if (content == null) {
				content = (TextView) baseView.findViewById(R.id.tv_content);
			}
			return content;
		}

		public TextView getSeriesNameTv() {
			if (seriesName == null) {
				seriesName = (TextView) baseView
						.findViewById(R.id.tv_series_name);
			}
			return seriesName;
		}

	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// final FeedBackEntity entity = (FeedBackEntity) getItem(position);
		// Intent intent = mActivity.getIntent();
		// intent.putExtra("feedbackEntity", entity);
		// mActivity.setResult(Activity.RESULT_OK, intent);
		// mActivity.finish();

	}

	@Override
	public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
			final int position, long arg3) {
		return false;
	}

}
